<!-- case列表 -->
<template>
    <div class="case">
        <child-table ref="tableInstance" title="选择case" :elTable_Options="elTable_Options" :flagIndex="props.flagIndex"
            @bottonHandle="opbuttonclick" @handleClick="handleClick" />
        <choose-component :choseData="choseData" ref="childchooseComponent" @inputsydata="inputsydata"
            v-if="modalStatus == '子列表弹框'" />
    </div>
</template>

<script setup>
import { ref, reactive, computed, toRefs, nextTick } from 'vue'
import { useRouter } from 'vue-router'
import commonV from '@/common'
import { useCach } from '@/store/cach'
import { ElMessage } from 'element-plus'

let props = defineProps({
    flagIndex: String
})

const router = useRouter()

let tableInstance = ref(null)
let childchooseComponent = ref(null)

let pageConfig = useCach()

let modalStatus = computed(() => pageConfig.modalStatus)

let elTable_Options = reactive({
    ifturnpage: false,
    showSummary: false,
    eltableArr: [{
        label: "case编号",
        prop: "case_no",
        columType: 'linkTo',
    },
    {
        label: "v1",
        prop: "v1",
        columType: 'text',
    },
    {
        label: "v2",
        prop: "v2",
        columType: 'text',
    },
    {
        label: "v3",
        prop: "v3",
        columType: 'text',
    },
    {
        label: "v4",
        prop: "v4",
        columType: 'text',
    },
    {
        label: "收入合计",
        prop: "total_price",
        columType: 'text'
    }],
    opbutton: [{
        label: '添加case',
        dealway: 'createdata',
        icon: 'Plus',
        type: "primary",
        keyV: "add",
        disabled: false
    }, {
        label: "删除",
        dealway: "deleteHandle",
        icon: 'Delete',
        type: "danger",
        keyV: "delete",
        disabled: false
    }],
})

let state = reactive({
    choseData: null,
    tableData: []
})
let { choseData } = toRefs(state)

let opbuttonclick = (wayname) => {
    if (wayname == 'createdata') {
        createdata()
        return
    }

    if (wayname == 'deleteHandle') {
        deleteHandle()
        return
    }
}

let createdata = () => {
    state.choseData = commonV.chooseComponent.vehiclecase
    pageConfig.changeShowModal(true)
    pageConfig.changeModalStatus("子列表弹框")
    nextTick(() => {
        let res = []
        if (commonV.ifrrandnotnone(state.tableData)) {
            res = JSON.parse(JSON.stringify(state.tableData))
        }
        childchooseComponent.value.openFunction(res)
    })
}

//删除
let deleteHandle = () => {
    let selectionTable = tableInstance.value.getSelection()
    if(!selectionTable.length){
        ElMessage({
            type: 'warning',
            message: '请选择删除项!'
        })
        return
    }

    let filter =  state.tableData.filter(it => {
        return !selectionTable.filter(i => i.id == it.id).length
    })
    setTableData(filter)
}

//公共数据带回
let inputsydata = (selectArr = []) => {
    setTableData(selectArr)
}

let handleClick = (row, index) => {
    router.push({
        path: `/syStandardLib/vehicleTestLibrary/view/${row.id}`
    })
}

let setTableData = (list = []) => {
    state.tableData = JSON.parse(JSON.stringify(list))
    tableInstance.value.setTableData(list)
}

defineExpose({ setTableData })

</script>

<style lang="scss" scoped></style>